package com.leessy.r229.server.http

import android.util.Log
import com.google.gson.Gson
import com.google.gson.TypeAdapter
import okhttp3.ResponseBody
import retrofit2.Converter

import java.io.IOException
import kotlin.jvm.Throws

/**
 * 解密服务器 body 数据
 */
class JsonResponseBodyConverter<T>(
    private val mGson: Gson?
    , private val adapter: TypeAdapter<T>?
) : Converter<ResponseBody, T> {
//    private var REGISTER_TOKEN: String by Preference(Constant.REGISTER_TOKEN_KEY, "")

    /**
     * 转换
     *
     * @param responseBody
     * @return
     * @throws IOException
     */

    @Throws(IOException::class)
    override fun convert(value: ResponseBody): T? {
        val response = value.string()
        Log.i("ResponseBodyConverter1:", "解密前的服务器数据：$response")
        //Log.i("ResponseBodyConverter1:", "解密的服务器数据：$REGISTER_TOKEN")
        var result = ""
//        val result = AESUtils.decrypt(response, REGISTER_TOKEN)//解密
//        val result =
//            if (Base64Utils.isBase64(response)) Base64Utils.decodedStr(response) else response//解密
        Log.i("ResponseBodyConverter:", "解密的服务器数据：$result")
        try {
            return adapter?.fromJson(result)
        } finally {
            value.close()
        }
    }

}